/**
 * Created by sicongdu on 17-11-21.
 */
var model_team = require("./model").team_message;
var students = require("../mongo_student").students
var teachers = require("../mongo_non_student").teachers
var admins = require("../mongo_non_student").admins
var donators = require("../mongo_non_student").donators

var query_message = (req, res) => {
  var message_list = null

  model_team.find({}, (err, documents) => {
    if (err) {
      res.send("mongodb error, team")
      return
    } else {
      message_list = JSON.parse(JSON.stringify(documents))
      console.log(message_list)

      var id_list = []
      for (var j = 0; j < message_list.length; j++) {
        id_list[j] = message_list[j].id
      }

      var role_list = []
      for (var x = 0; x < message_list.length; x++) {
        role_list[x] = message_list[x].role
      }

      // var id_set = new Set(message_list)
      var flag = []
      for (var m = 0; m < message_list.length; m++) {
        flag[m] = 0
      }

      for (var i = 0; i < message_list.length; i++) {
        var where = {}

        if (role_list[i] == "1") {

          where = {
            studentNo: message_list[i].id
          }

          students.findOne(where, (err, docu) => {
            if (err) {
              res.send("mongodb error, user")
              return
            } else {
              for (var k = 0; k < message_list.length; k++) {
                if (message_list[k].id == docu.studentNo) {
                  message_list[k].id = docu.name
                  flag[k] = 1
                }
              }

              var sum = 0
              for (var n = 0; n < flag.length; n++) {
                sum += flag[n]
              }
              if (sum == message_list.length) {
                res.send(message_list)
                return
              }
            }
          })

        } else if (role_list[i] == "2") {

          where = {
            teacherNo: message_list[i].id
          }

          teachers.findOne(where, (err, docu) => {
            if (err) {
              res.send("mongodb error, user")
              return
            } else {
              for (var k = 0; k < message_list.length; k++) {
                if (message_list[k].id == docu.teacherNo) {
                  message_list[k].id = docu.name
                  flag[k] = 1
                }
              }

              var sum = 0
              for (var n = 0; n < flag.length; n++) {
                sum += flag[n]
              }
              if (sum == message_list.length) {
                res.send(message_list)
                return
              }
            }
          })

        } else if (role_list[i] == "3") {

          where = {
            adminNo: message_list[i].id
          }

          admins.findOne(where, (err, docu) => {
            if (err) {
              res.send("mongodb error, user")
              return
            } else {
              for (var k = 0; k < message_list.length; k++) {
                if (message_list[k].id == docu.adminNo) {
                  message_list[k].id = docu.name
                  flag[k] = 1
                }
              }

              var sum = 0
              for (var n = 0; n < flag.length; n++) {
                sum += flag[n]
              }
              if (sum == message_list.length) {
                res.send(message_list)
                return
              }
            }
          })

        } else if (role_list[i] == "4") {

          where = {
            donatorNo: message_list[i].id
          }

          donators.findOne(where, (err, docu) => {
            if (err) {
              res.send("mongodb error, user")
              return
            } else {
              for (var k = 0; k < message_list.length; k++) {
                if (message_list[k].id == docu.donatorNo) {
                  message_list[k].id = docu.name
                  flag[k] = 1
                }
              }

              var sum = 0
              for (var n = 0; n < flag.length; n++) {
                sum += flag[n]
              }
              if (sum == message_list.length) {
                res.send(message_list)
                return
              }
            }
          })

        }
      }
    }
  })
}


module.exports = {
  query_message: query_message
}
